package com.java.springboot.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhangtengfei
 * @date 2021/9/29 15:31
 */

@Configuration
public class ElasticSearchConfig {
    /**
     * restHighLevelClient.search(SearchRequest searchRequest, RequestOptions options)
     * 官方建议将 RequestOptions 做成单实例的，以后的所有对ES的请求都能共享使用这个实例
     */
    public static final RequestOptions COMMON_OPTIONS;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        // 这里可以开启安全校验
        COMMON_OPTIONS = builder.build();
    }
    
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestClientBuilder builder = RestClient.builder(
                // 这里可以有多个，集群配置
                new HttpHost("82.156.203.50", 9200, "http")
        );
        return new RestHighLevelClient(builder);
    }
}
